package vf1.iu;

/************************************************************************
 VF1_IU_Inicio.java

 BattleField Tactics Online - A Strategy RPG Online Game
 Copyright (C) 2008  
 Douglas Ribeiro, Kely Marques Rosa, Rodrigo da Fonseca Lacerda

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 Contact: douglas666@gmail.com
 **************************************************************************/

import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import vf1.so.*;

/**
 * Class VF1_IU_Inicio
 */
public class VF1_IU_Inicio extends JPanel implements MouseListener { 
	// -
	// Detalhar
	// isso
	// no MI

	//
	// Fields
	//

	/**
	 * 
	 */

	private JFrame frame = new JFrame(); 
	private JLayeredPane pane = new JLayeredPane(); 

	private static final long serialVersionUID = 1L; //  Só para
	// tirar warning
	private String usuario;
	private String servidor;
	private JButton conectar = new JButton("Jogar"); 
	private JTextField usuario_form = new JTextField(15);
	private JTextField servidor_form = new JTextField(15); 

	//
	// Constructors
	//
	public VF1_IU_Inicio() {

	};

	//
	// Methods
	//

	//
	// Accessor methods
	//

	/**
	 * Set the value of usuario
	 * 
	 * @param newVar
	 *            the new value of usuario
	 */
	private void setUsuario(String newVar) {
		usuario = newVar;
	}

	/**
	 * Get the value of usuario
	 * 
	 * @return the value of usuario
	 */
	private String getUsuario() {
		return usuario;
	}

	/**
	 * Set the value of servidor
	 * 
	 * @param newVar
	 *            the new value of servidor
	 */
	private void setServidor(String newVar) {
		servidor = newVar;
	}

	/**
	 * Get the value of servidor
	 * 
	 * @return the value of servidor
	 */
	private String getServidor() {
		return servidor;
	}

	/**
	 * Set the value of conectar
	 * 
	 * @param newVar
	 *            the new value of conectar
	 */
	private void setConectar(JButton newVar) {
		conectar = newVar;
	}

	/**
	 * Get the value of conectar
	 * 
	 * @return the value of conectar
	 */
	private JButton getConectar() {
		return conectar;
	}

	/**
	 * Set the value of usuario_form
	 * 
	 * @param newVar
	 *            the new value of usuario_form
	 */
	private void setUsuario_form(JTextField newVar) {
		usuario_form = newVar;
	}

	/**
	 * Get the value of usuario_form
	 * 
	 * @return the value of usuario_form
	 */
	private JTextField getUsuario_form() {
		return usuario_form;
	}

	/**
	 * Set the value of servidor_form
	 * 
	 * @param newVar
	 *            the new value of servidor_form
	 */
	private void setServidor_form(JTextField newVar) {
		servidor_form = newVar;
	}

	/**
	 * Get the value of servidor_form
	 * 
	 * @return the value of servidor_form
	 */
	private JTextField getServidor_form() {
		return servidor_form;
	}

	//
	// Other methods
	//

	/**
	 */
	public void minimizarTela() {
		//Responsabilidade Automática do Java

		//MA: minimiza a janela;
	}

	/**
	 */
	public void fecharTela() {
		//MA: fecha janela
	}

	/**
	 */
	public void apresentarTelaInicio() // Douglas -
	// alterado
	{
		/*MA
		apresenta uma janela em java;
	    apresenta formulario para colocar usu�rio;
	    apresenta formulario para colocar servidor;
	    imprime "usuario" na tela;
	    imprime "servidor" na tela;
	    adiciona bot�o "jogar" � janela;*/

		JFrame f = getFrame();
		setBackground(Color.lightGray);
		setForeground(Color.black);
		setFont(new Font("Dialog", Font.BOLD, 40));

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JLayeredPane p = getPane();

		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		p.add(Box.createRigidArea(new Dimension(800, 600)));
		this.add(p);
		f.setContentPane(this);
		f.pack();
		int w = f.getWidth();
		int h = f.getHeight();
		p.removeAll();
		p.add(Box.createVerticalStrut(h - 600 + 400));
		JLabel text = new JLabel();
		text.setText("Usuário:");
		p.add(text);
		this.getServidor_form().setEditable(true);
		p.add(this.getUsuario_form());
		JLabel text1 = new JLabel();
		text1.setText("Servidor:");
		p.add(text1);
		p.add(this.getServidor_form());
		this.getConectar().addMouseListener(this);
		p.add(this.getConectar());
		this.add(p);

		System.out.printf("%d %d", w, h);

		p.setOpaque(false);

		f.setContentPane(this);
		f.setSize(w, h);
		f.setResizable(false);
		f.setVisible(true);

	}

	/**
	 */
	public void coletarInformacoes() {

		// MA: coleta as informa��es nos formularios quando apertar bot�o jogar;
		setUsuario(getUsuario_form().getText());
		setServidor(getServidor_form().getText());
	}

	public void paintComponent(Graphics g) { // Douglas - Novo método no MI

		super.paintComponents(g);
		if (g instanceof Graphics2D) {
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
					RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		}
		g.drawString("BattleField Tactics", 100, 250);
	}

	@Override
	public void mouseClicked(MouseEvent e) { // Douglas - Temporário para
		// teste de telas
		// TODO Auto-generated method stub
		getFrame().dispose();
		this.coletarInformacoes();

		VF1_SO_BFTCliente cliente = VF1_SO_BFTCliente.getCliente();

		cliente.iniciarTelaEspera();

		try {
			cliente.conectarAoServidor(getUsuario(), getServidor());
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block

			e1.printStackTrace();
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NotBoundException e1) {
			// TODO Auto-generated catch block

			e1.printStackTrace();
		}


		/*try {
			wait(30000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		//System.out.print("Acabou");
		/*
		 * getScreen_inicio().restoreScreen(); VF1_SO_BFTCliente cliente =
		 * VF1_SO_BFTCliente.getCliente(); cliente.iniciarTelaEspera();
		 */
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	public JLayeredPane getPane() {
		return pane;
	}

	public void setPanel(JLayeredPane pane) {
		this.pane = pane;
	}

}
